package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    CategoryMapper categoryMapper;
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page=setmealMapper.selectPage(setmealPageQueryDTO);
        page.getResult().forEach(setmealVO -> {
            setmealVO.setCategoryName(categoryMapper.selectTypeNameById(setmealVO.getCategoryId()));;
        });
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmeal.setCreateTime(LocalDateTime.now());
        setmeal.setUpdateTime(LocalDateTime.now());
        setmealMapper.insert(setmeal);
        setmealDTO.getSetmealDishes().forEach(setmealDish->{
            setmealDish.setSetmealId(setmeal.getId());
        });
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealMapper.insertSetmealDishes(setmealDishes);
    }

    @Override
    public void startOrStop(String status, String id) {
        setmealMapper.startOrStop(status,id);
    }

    @Transactional
    @Override
    public void delectById(String ids) {

        String[] split = ids.split(",");
        System.out.println(split);
        for (String s : split) {
            Setmeal setmeals=setmealMapper.selectById(s);
            if (setmeals.getStatus()!=0){
                throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        setmealMapper.delectById(split);
        setmealMapper.delectDishById(split);

    }

    @Override
    public SetmealVO getById(String id) {
        Setmeal setmeal = setmealMapper.selectById(id);
        SetmealVO setmealVO=new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealMapper.selectSetmealDishesById(id));
        return setmealVO;
    }

    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        setmealMapper.update(setmealDTO);
        setmealDTO.getSetmealDishes().forEach(setmealDish->{
            setmealDish.setSetmealId(setmealDTO.getId());
        });
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealMapper.delectDishByIds(setmealDTO.getId());
        setmealMapper.insertSetmealDishes(setmealDishes);

    }
    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
